define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'notice/index' + location.search,
                    add_url: 'notice/add',
                    edit_url: 'notice/edit',
                    del_url: 'notice/del',
                    multi_url: 'notice/multi',
                    import_url: 'notice/import',
                    table: 'notice',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cover', title: __('Cover'),formatter: Controller.api.formatter.articleCover,events: Controller.api.events.previewVideo},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'type', title: __('Type'), operate: 'LIKE', searchList: {'1': '文本','2': '视频'}, formatter: Controller.api.formatter.type},
                        {field: 'author', title: __('Author'), operate: 'LIKE'},
                        {field: 'look_num', title: __('Look_num')},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                type: function (value){
                    switch (value){
                        case '1':
                            return '<span class="label label-danger">文本</spam>';
                        case '2':
                            return '<span class="label label-info">视频</spam>';
                    }
                },
                articleCover: function (value, row){
                    if (row.type == 1){
                        return "<button class='btn btn-bitbucket btn-xs preview-article-video'>预览内容</button>";
                    }else {
                        return "<button class='btn btn-bitbucket btn-xs preview-article-video'>预览视频</button>";
                    }
                }
            },
            // 配置事件
            events: {
                // 事件名称
                previewVideo:{
                    'click button.preview-article-video':function (e,value){
                        if (value == ''){
                            layer.msg('暂无视频');
                            return false;
                        }
                        layer.open({
                            type:2,
                            title:'预览视频',
                            content:value,
                            area:['800px','600px']
                        });
                    }
                }
            }
        }
    };
    return Controller;
});
